package com.hibernate.learn3;

import com.hibernate.learn3.model.Address;
import com.hibernate.learn3.model.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        Configuration configuration = new Configuration().configure("1hibernate.cfg.xml");
        configuration.addAnnotatedClass(User.class);
        configuration.addAnnotatedClass(Address.class);

        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(registry);

        Address address = new Address();
        address.setCity("Kiev");
        address.setStreet("Hello street");
        address.setNumber(334);

        User user = new User();
        user.setName("Alex");
        user.setAddress(address);

        User user1 = new User();
        user1.setName("Katya");
        user1.setAddress(address);

        User user2 = new User();
        user2.setName("Vika");
        user2.setAddress(address);


        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user1);
        users.add(user2);

        for (User u : users) {
            setUserToNewAddress(sessionFactory, u);
        }

        boolean status = deleteUserByUserIdAddressId(sessionFactory, 4, 2);
        if (status) {
            System.out.println("User was deleted");
        } else {
            System.out.println("User not deleted");
        }

        sessionFactory.close();
    }

    public static void setUserToNewAddress(SessionFactory sessionFactory, User user) {

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        session.save(user);

        session.getTransaction().commit();
        session.close();

    }


    public static void addUserToAddress(SessionFactory sessionFactory, User user, Address address) {

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        address.setUser(user);
        user.setAddress(address);

        session.save(user);

        session.getTransaction().commit();
        session.close();

    }

    public static Address getAddressById(SessionFactory sessionFactory, int address_id) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Address address = session.find(Address.class, address_id);

        session.getTransaction().commit();
        session.close();

        return address;
    }

    public static User getUserById(SessionFactory sessionFactory, int user_id) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        User user = session.find(User.class, user_id);

        session.getTransaction().commit();
        session.close();

        return user;
    }

    public static boolean deleteUserByUserIdAddressId(SessionFactory sessionFactory, int user_id, int address_id) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        User user = session.find(User.class, user_id);
        Address address = session.find(Address.class, address_id);
        if (user == null || address == null) {
            session.close();
            return false;
        }

        address.removeUser(user);
        session.getTransaction().commit();

        session.close();
        return true;
    }

}
